<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
</head>

<body>
    <div id="app">
        <ul>
            <li v-for="(item,key) in list" :key="item.id"><div @click="jump(item.src,item.id)">{{item.nav}}</div></li>
        </ul>
        <router-view></router-view>
    </div>

    <script>
        // 路由需要的组件
        var index = Vue.component('index',{
            template: `<div>第一页</div>`,
        })
        var other = Vue.component('other',{
            template: `<div>其他页</div>`,
        })

        // 创建路由对象
        var router = new VueRouter({
            routes: [
                {name: 'index', path: '/index/:id', component: index},
                {name: 'other', path: '/other/:id', component: other},
                {name: 'default', path: '*', redirect: {name: 'index'}},
            ]
        })

        var vm = new Vue({
            el: '#app',
            router,
            data: {
                list: [
                    {id:1,src:'/index',nav:"首页"},
                    {id:2,src:'/other',nav:"其他页"},
                    {id:3,src:'/other',nav:"其他页"},
                ]
            },
            methods: {
                jump(index, id) {
                    this.$router.push({path:index+'/'+id})
                }
            }
        });
    </script>
</body>

</html>